<?php


namespace app\admin\controller;


use app\common\controller\Adminbase;
use EasyWeChat\Factory;
use think\Db;
use think\facade\Session;

class Orderjf extends Adminbase
{
    protected function initialize()
    {
        parent::initialize();
    }
    public function index()
    {
        if ($this->request->isAjax()) {
            list($page, $limit) = $this->buildTableParames();
            $where[] = ['ojr.id', '>', 0];
            $filter = input('filter');
            if ($filter) {
                $filter = json_decode($filter, true);
                if (isset($filter['status'])) {
                    $where[] = ['ojr.status', '=', $filter['status']];
                }
                if (isset($filter['pay_status'])) {
                    $where[] = ['o.pay_status', '=', $filter['pay_status']];
                }

                if (isset($filter['pay_type'])) {
                    $where[] = ['o.pay_type', '=', $filter['pay_type']];
                }
                if (isset($filter['delive_type'])) {
                    $where[] = ['o.delive_type', '=', $filter['delive_type']];
                }

                if (isset($filter['order_no'])) {
                    $where[] = ['o.order_no', 'like', '%' . $filter['order_no'] . '%'];
                }
                if (isset($filter['nickname'])) {
                    $where[] = ['m.nickname', 'like', '%' . $filter['nickname'] . '%'];
                }
                if (isset($filter['province'])) {
                    $province = Db::name('area')->find($filter['province']);
                    $where[] = ['h.province', '=', $province['name']];
                }
                if (isset($filter['city'])) {
                    $city = Db::name('area')->find($filter['city']);
                    $where[] = ['h.city', '=', $city['name']];
                }
                if (isset($filter['create_time'])) {
                    $create_time = explode(' - ', $filter['create_time']);
                    foreach ($create_time as $k => $v) {
                        $create_time[$k] = strtotime($v);
                    }
                    $where[] = ['ojr.create_time', 'between', [$create_time[0], $create_time[1]]];
                }
                if (isset($filter['pay_time'])) {
                    $create_time = explode(' - ', $filter['pay_time']);
                    foreach ($create_time as $k => $v) {
                        $create_time[$k] = strtotime($v);
                    }
                    $where[] = ['o.pay_time', 'between', [$create_time[0], $create_time[1]]];
                }
                if (isset($filter['time'])) {
                    $create_time = explode(' - ', $filter['time']);
                    foreach ($create_time as $k => $v) {
                        $create_time[$k] = strtotime($v);
                    }
                    $where[] = ['o.time', 'between', [$create_time[0], $create_time[1]]];
                }
                if (isset($filter['pay_time'])) {
                    $create_time = explode(' - ', $filter['pay_time']);
                    foreach ($create_time as $k => $v) {
                        $create_time[$k] = strtotime($v);
                    }
                    $where[] = ['o.pay_time', 'between', [$create_time[0], $create_time[1]]];
                }
            }
            //获取列表
            $_list = Db::name('order_jf_return')->alias('ojr')
                ->join('member m', 'ojr.member_id=m.id')
                ->join('order o','ojr.order_id=o.id')
                ->field('o.id as order_id,o.order_no,m.nickname,m.avatarUrl,m.mobile,ojr.*')
                ->where($where)
                ->page($page, $limit)
                ->order('ojr.create_time desc')
                ->select();
            foreach ($_list as $k => $v) {
                setGetJf($v['order_id']);
                $_list[$k]['create_time'] = date('Y-m-d H:i:s', $v['create_time']);
                $order_goods= Db::name('order_goods')->where('order_id','=',$v['order_id'])->find();
                $_list[$k]['goods_info']=$order_goods['goods_name'].'/'.$order_goods['suk'].'/'.$order_goods['num'];
            }
            //获取数量
            $total = Db::name('order_jf_return')->alias('ojr')
                ->join('member m', 'ojr.member_id=m.id')
                ->join('order o','ojr.order_id=o.id')
                ->where($where)
                ->count();
            $result = ["code" => 0, "count" => $total, "data" => $_list];
            return json($result);
        }
        return $this->fetch();
    }

    public function edit()
    {
        if ($this->request->isPost()) {
            $data = $this->request->post();
            $stmt = Db::name('order')->where('id', $data['id'])->update($data['row']);
            $this->success('操作成功！');
        }
        $id = $this->request->param('id/d', 0);
        $data = Db::name('order')->find($id);
        $this->assign('data', $data);
        return $this->fetch('');

    }


    public function jf_list(){
        $order_id = $this->request->param('order_id/d', 0);
        if ($this->request->isAjax()) {
            list($page, $limit) = $this->buildTableParames();
            $where[]=['order_id','=',$order_id];
            $_list=Db::name('order_jf_return_detail')
                ->where($where)
                ->order('day')
                ->page($page,$limit)
                ->select();
            foreach ($_list as $k=>$v){
                $_list[$k]['create_time'] = date('Y-m-d H:i:s', $v['create_time']);
                if ($v['get_time']){
                    $_list[$k]['get_time'] = date('Y-m-d H:i:s', $v['get_time']);
                }else{
                    $_list[$k]['get_time'] = '------';
                }
            }
            $total=Db::name('order_jf_return_detail')
                ->where($where)
                ->count();
            $result = ["code" => 0, "count" => $total, "data" => $_list];
            return json($result);
        }
        $this->assign([
            'order_id'=>$order_id
        ]);
        return $this->fetch();
    }


}